package me.search;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(search(arr, 4));
    }

    public static int search(int[] arr, int target) {
        return recursion(arr, target, 0, arr.length - 1);
    }

    public static int recursion(int[] arr, int target, int i, int j) {
        // 递归出口
        if (i > j) {
            return -1;
        }
        int m = (i + j) >>> 1;
        if (target < arr[m]) {
            return recursion(arr, target, i, m - 1);
        } else if (arr[m] < target) {
            return recursion(arr, target, m + 1, j);
        } else {
            return m;
        }
    }
}
